// pages/index/index.js
var API = require("../../config/api.js");
var UTILS = require("../../utils/util.js");

var APP = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    RESOURCE_ROOT: "",
    categoryList: [],
    shopList: []
  },
  /**
   * 
   */
  toShopList: function () {
    wx.navigateTo({
      url: '/pages/shop/list/list'
    });
  },
  selectShop: function (event) {
    var dataset = event.currentTarget.dataset;
    var shopId = dataset.shopid;
    wx.navigateTo({
      url: '/pages/shop/index/index?shopId=' + shopId,
      success: function(res){ 
        // success
      }
    });
  },
  getCategoryListFromServer: function() {
    var _this = this;
    wx.request({
      url: API.GET_CATE_LIST_URL + "/1/10",
      method: 'GET',
      success: function(res){
        var data = res.data;
        if (data.state.code === API.OK) {
          _this.setData({
            categoryList: data.result.records
          });
        } else if (data.state.code === API.FAIL) {
          UTILS.showErrorToast(data.state.msg);
        }  
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  getShopListFromServer: function() {
    var _this = this;
    wx.request({
      url: API.GET_SHOP_LIST_URL + "/1/2",
      method: 'GET',
      success: function(res){
        var data = res.data;
        if (data.state.code === API.OK) {
          _this.setData({
            shopList: data.result.records
          });
        } else if (data.state.code === API.FAIL) {
          UTILS.showErrorToast(data.state.msg);
        }
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      RESOURCE_ROOT: API.RESOURCE_ROOT
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getCategoryListFromServer();
    this.getShopListFromServer();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})